using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Text;

namespace GlobalLib
{
    public class TcpHelper
    {
        public string hostname;

        public int port;

        public TcpHelper(string hostname, int port)
        {
            this.hostname = hostname;
            this.port = port;
        }

        public void Send(object obj)
        {
            try
            {
                using (TcpClient client = new TcpClient(hostname, port))
                {

                    using (MemoryStream stream = new MemoryStream())
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, obj);
                        NetworkStream s = client.GetStream();
                        byte[] b = stream.GetBuffer();
                        client.SendTimeout = 2000;
                        s.Write(b, 0, b.Length);
                    }
                }
            }
            catch { }
        }
    }
}
